<?php
include_once("../Dao/db.php");
Class Export
{
	var $camp=-1;
	
	var $campList="<option value='-1' >(指定なし)";
	
	var $search_status=-1;
	
	var $actionName=-1;
	
	function MyTask()
	{
		session_start();
		$this->iniForm();
		$this->getCamp();

	}
	
	function iniForm()
	{
		if(isset($_POST["search_campaign_select"])) $this->camp = $_POST["search_campaign_select"];
		if(isset($_POST["search_status"])) $this->search_status = $_POST["search_status"];
		
		if(isset($_POST["actionName"])) 
		{
			$this->actionName = $_POST["actionName"];
			$this->exportCSV();
		}
				
		
	}
	
	function getCamp()
	{
		$sql = "select camp_Id,name from campaign ";
		$sql .="where memberId = ".$_SESSION["memberId"]." ";
		$sql .=" and status<>9 ";
		//echo $sql;
		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);
		if ($rows > 0)
		{
			for ($i=0;$i<$rows;$i++)
			{
				$data = mysql_fetch_row($result);				
				$this->campList = $this->campList."<option id=".$i." value=".$data[0].">".$data[1]."</option>";
			}			
		}
	}
	
	
	function exportCSV(){
		// ファイル名を$mcsvと指定
		header("Content-Disposition: attachment; filename=".$this->actionName.".csv");
		// ダウンロード処理開始
		header("Content-Type: application/octet-stream;name=".$this->actionName.".csv");

		
		if($this->actionName == 'Adgroup'){
			$sql = "SELECT campaign.name,adgroup.name,";
			$sql .= "networkTargeting,adgroup.maxcpc,networkTargeting,adgroup.maxContentCpc,";
			$sql .= "IF( adgroup.status =0, 'OFF', 'ON' )";
			$sql .= " FROM adgroup,campaign ";
			$sql .= " where adgroup.memberId = ".$_SESSION["memberId"];
			$sql .= " and campaign.camp_Id =  adgroup.camp_Id ";
			if($this->camp !=-1) $sql .= " AND  adgroup.camp_Id=".$this->camp ;
			if($this->search_status !=-1) $sql .= " AND  adgroup.status=".$this->search_status ;
			$sql .= " AND adgroup.status <> 9 AND campaign.status <> 9 ";
			//echo '\"'.$sql.'\"';
			echo "#AdWords\n";
			echo "#\n";
			echo mb_convert_encoding("#キャンペーン名,広告グループ名,スポンサードサーチ,入札額,コンテンツマッチ,入札額,ステータス\n","SJIS","UTF-8");
			$result = mysql_query($sql);	
			$fields = mysql_num_fields($result);
			if(!$fields) exit;
			while($row = mysql_fetch_array($result)) {
			   for($j=0; $j< $fields; $j++) {
			   		if($j ==  2)
					{
						if(strpos(addslashes($row[$j]), 'GoogleSearch')!== false || strpos(addslashes($row[$j]), 'SearchNetwork')!== false )
						{
							echo 'ON,';
						}
						else
						{
							echo 'OFF,';
						}
					}
					elseif($j ==  4)
					{
						if(strpos(addslashes($row[$j]), 'ContentNetwork')!== false )
						{
							echo 'ON,';
						}
						else
						{
							echo 'OFF,';
						}
					}
					else
					{
						echo mb_convert_encoding('"'.addslashes($row[$j]).'"',"SJIS","UTF-8");
						if($j < $fields-1) echo ',';
					}
			   }
			   echo "\n";
			}
			
		}else if ($this->actionName == 'Creative'){
			$sql = "SELECT campaign.name,adgroup.name,creative.name,
    					headline,description1,description2,displayurl,
    					creative.destinationUrl,IF( creative.status =0, 'OFF', 'ON' )
					FROM campaign, adgroup, creative ";
			$sql .=	" WHERE creative.memberId = ".$_SESSION["memberId"];
			$sql .=	" 	AND campaign.camp_Id = adgroup.camp_Id 
						AND creative.adGr_Id = adgroup.adGr_Id
						AND creative.status <> 9 ";
			if($this->camp !=-1) $sql .= " AND  adgroup.camp_Id=".$this->camp ;
			if($this->search_status !=-1) $sql .= " AND  creative.status=".$this->search_status ;
			echo "#AdWords\n";
			echo "#\n";
			echo mb_convert_encoding("#キャンペーン名,広告グループ名,クリエイティブ名,タイトル,説明文,説明文2,表示URL,ランディング先URL,ステータス\n","SJIS","UTF-8");
			$result = mysql_query($sql);	
			$fields = mysql_num_fields($result);
			if(!$fields) exit;
			while($row = mysql_fetch_array($result)) {
			   for($j=0; $j< $fields; $j++) {
				// $mcsvファイルにデータを書き出し
					//echo '"'.addslashes($row[$j]).'"';
					echo mb_convert_encoding('"'.addslashes($row[$j]).'"',"SJIS","UTF-8");
					if($j < $fields-1) echo ',';
			   }
			   echo "\n";
			}
		}else {
			$sql = "SELECT campaign.name,adgroup.name,keyword.text,
 					case keyword.type
    					when 1 then '完全一致'
    					when 2 then '部分一致・完全一致'
    					else '部分一致・完全一致'
  					end type,
 					keyword.maxcpc,
 					keyword.destinationUrl,
 					IF( keyword.status =0, 'OFF', 'ON' ) status
					FROM campaign, adgroup, keyword ";
			$sql .=	" where campaign.memberId = ".$_SESSION["memberId"];
			$sql .=	" AND campaign.camp_Id = adgroup.camp_Id 
					  AND keyword.adGr_Id = adgroup.adGr_Id
					  AND keyword.negative <> 1 AND keyword.status <> 9 ";
			if($this->camp !=-1) $sql .= " AND  adgroup.camp_Id=".$this->camp ;
			if($this->search_status !=-1) $sql .= " AND  keyword.status=".$this->search_status ;
			//echo '\"'.$sql.'\"';
			echo "#AdWords\n";
			echo "#\n";
			echo mb_convert_encoding("#キャンペーン名,広告グループ名,キーワード,マッチ,単価,ランディング先URL,ステータス\n","SJIS","UTF-8");	
			$result = mysql_query($sql);	
			$fields = mysql_num_fields($result);
			if(!$fields) exit;
			while($row = mysql_fetch_array($result)) {
			   for($j=0; $j< $fields; $j++) {
				// $mcsvファイルにデータを書き出し
					//echo '"'.addslashes($row[$j]).'"';
					echo mb_convert_encoding('"'.addslashes($row[$j]).'"',"SJIS","UTF-8");
					if($j<$fields-1) echo ',';
			   }
			   echo "\n";
			}		
		}
		

		exit;
	}
	
	
	
}

